//
//  HRMyCollectViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-11.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRMyCollectViewController.h"
#import "HRMyCollectTableViewCell.h"
#import "HRShopCartModel.h"
#import "HRGoodsInfoViewController.h"

@interface HRMyCollectViewController (){
    NSMutableArray *arrayTable;

}

@end

@implementation HRMyCollectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        arrayTable = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"我的收藏";
}

-(void)viewWillAppear:(BOOL)animated{
    [self loadCollectList];
    
    [super viewWillAppear:animated];
}

-(void)loadCollectList{
    
    //隐藏提示页面
    [self hideHud];
    
    [arrayTable removeAllObjects];
    
    [self showCustomLoadingHud:kLoading inView:nil];
    [HRInterfaceManager getFavoriteListByUid:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        if (isSucceed) {
            HRShopCartModel *model = (HRShopCartModel*)data;
            if (model.list &&model.list.count) {
                [arrayTable addObjectsFromArray:model.list];
                [_tableView reloadData];
            }
        }else if([message isEqualToString:NO_WIFI_ERROR_CODE]){
            //显示无网络提示
            [self showHudByType:HUDTYPE_NO_WIFI];
            return;
        }else{
            [self showTextHUD:message];
        }
        
        if (!arrayTable.count) {
            //显示为空提示
            [self showHudByType:HUDTYPE_FAVORITE];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - TableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayTable.count;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HRMyCollectTableViewCell";
    HRMyCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HRMyCollectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell configWithData:[arrayTable objectAtIndex:indexPath.row]];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self showCustomLoadingHud:kLoading inView:nil];
    HRProductInfo *info = [arrayTable objectAtIndex:indexPath.row];
    [HRInterfaceManager deleteFavorite:info.productId completion:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        if (isSucceed) {
            [arrayTable removeObjectAtIndex:indexPath.row];
            [_tableView reloadData];
            if (arrayTable.count == 0) {
                [self showHudByType:HUDTYPE_FAVORITE];
            }
        }
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HRProductInfo *info = [arrayTable objectAtIndex:indexPath.row];
    HRGoodsInfoViewController *viewC = [[HRGoodsInfoViewController alloc] initWithNibName:@"HRGoodsInfoViewController" bundle:nil];
    viewC.goodsId = info.productId;
    [self.navigationController pushViewController:viewC animated:YES];

}


#pragma mark - HRProgress Delegate Methods
//到处逛逛
-(void)tapHUDButtonAction:(id)button{
    UIButton *theButton=(UIButton *)button;
    
    if ([theButton.titleLabel.text isEqualToString:@"重新加载"]) {
        //重新加载页面
        [self loadCollectList];
    }else{
        //去逛逛
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
